import 'dart:convert';

import 'package:ride_rex/article/comment.dart';
import 'package:ride_rex/main/home/landmark_bean.dart';

const String jsonKeyFeedUserBeanUserId = 'userId';
const String jsonKeyFeedUserBeanNickname = 'nickname';
const String jsonKeyFeedUserBeanAvatar = 'avatar';
const String jsonKeyFeedUserBeanIsFriend = 'isFriend';
const String jsonKeyFeedUserBeanAuthor = 'author';
const String jsonKeyFeedUserBeanLocalVideo = 'local_video';
const String jsonKeyFeedUserBeanLocalVideoCover = 'local_video_cover';
const String jsonKeyFeedUserBeanVideoTitle = 'video_title';
const String jsonKeyFeedUserBeanVideoId = 'video_id';
const String jsonKeyFeedUserBeanWorks = 'works';
const String jsonKeyFeedUserBeanIsVideoLike = 'is_video_like';
const String jsonKeyFeedUserBeanLandMarks = 'landmarks';
const String jsonKeyFeedUserBeanIsBlock = 'is_block';
const String jsonKeyFeedUserBeanSortId = 'sort_id';

class FeedUserBean {
  String? userId;
  String? nickname;
  String? avatar;
  bool? isFriend;
  int? sortId;

  ///本地json中的字段
  String? author;
  String? localVideo;
  String? localVideoCover;
  String? videoTitle;
  String? videoId;

  List<FeedUserWorkBean>? works;

  ///扩展字段
  bool isVideoLike = false; //是否点赞视频
  bool isBlock = false; //是否拉黑
  List<LandmarkBean>? landmarks; //解锁的地标

  List<CommentBean> videoCommentList = [];

  FeedUserBean({
    this.userId,
    this.nickname,
    this.avatar,
    this.isFriend,
    this.author,
    this.localVideo,
    this.localVideoCover,
    this.works,
    this.isVideoLike = false,
    this.isBlock = false,
    this.landmarks,
    this.videoTitle,
    this.videoId,
    this.sortId,
  });

  FeedUserBean.fromJson(dynamic json) {
    if (json[jsonKeyFeedUserBeanUserId] is String) {
      userId = json[jsonKeyFeedUserBeanUserId];
    }
    if (json[jsonKeyFeedUserBeanNickname] is String) {
      nickname = json[jsonKeyFeedUserBeanNickname];
    }
    if (json[jsonKeyFeedUserBeanAvatar] is String) {
      avatar = json[jsonKeyFeedUserBeanAvatar];
    }
    if (json[jsonKeyFeedUserBeanIsFriend] is bool) {
      isFriend = json[jsonKeyFeedUserBeanIsFriend];
    }
    if (json[jsonKeyFeedUserBeanAuthor] is String) {
      author = json[jsonKeyFeedUserBeanAuthor];
    }
    if (json[jsonKeyFeedUserBeanLocalVideo] is String) {
      localVideo = json[jsonKeyFeedUserBeanLocalVideo];
    }
    if (json[jsonKeyFeedUserBeanLocalVideoCover] is String) {
      localVideoCover = json[jsonKeyFeedUserBeanLocalVideoCover];
    }
    if (json[jsonKeyFeedUserBeanWorks] is List) {
      works = json[jsonKeyFeedUserBeanWorks] == null
          ? null
          : (json[jsonKeyFeedUserBeanWorks] as List).map((e) => FeedUserWorkBean.fromJson(e)).toList();
    }
    if (json[jsonKeyFeedUserBeanIsVideoLike] is bool) {
      isVideoLike = json[jsonKeyFeedUserBeanIsVideoLike];
    }
    if (json[jsonKeyFeedUserBeanLandMarks] is List) {
      landmarks = json[jsonKeyFeedUserBeanLandMarks] == null
          ? null
          : (json[jsonKeyFeedUserBeanLandMarks] as List).map((e) => LandmarkBean.fromJson(e)).toList();
    }
    if (json[jsonKeyFeedUserBeanIsBlock] is bool) {
      isBlock = json[jsonKeyFeedUserBeanIsBlock];
    }
    if (json[jsonKeyFeedUserBeanVideoTitle] is String) {
      videoTitle = json[jsonKeyFeedUserBeanVideoTitle];
    }
    if (json[jsonKeyFeedUserBeanVideoId] is String) {
      videoId = json[jsonKeyFeedUserBeanVideoId];
    }
    if (json[jsonKeyFeedUserBeanSortId] is int) {
      sortId = json[jsonKeyFeedUserBeanSortId];
    }
  }

  FeedUserBean.fromDbJson(dynamic json) {
    if (json[jsonKeyFeedUserBeanUserId] is String) {
      userId = json[jsonKeyFeedUserBeanUserId];
    }
    if (json[jsonKeyFeedUserBeanNickname] is String) {
      nickname = json[jsonKeyFeedUserBeanNickname];
    }
    if (json[jsonKeyFeedUserBeanAvatar] is String) {
      avatar = json[jsonKeyFeedUserBeanAvatar];
    }
    if (json[jsonKeyFeedUserBeanIsFriend] is String) {
      isFriend = json[jsonKeyFeedUserBeanIsFriend] == '0' ? false : true;
    }
    if (json[jsonKeyFeedUserBeanAuthor] is String) {
      author = json[jsonKeyFeedUserBeanAuthor];
    }
    if (json[jsonKeyFeedUserBeanLocalVideo] is String) {
      localVideo = json[jsonKeyFeedUserBeanLocalVideo];
    }
    if (json[jsonKeyFeedUserBeanLocalVideoCover] is String) {
      localVideoCover = json[jsonKeyFeedUserBeanLocalVideoCover];
    }
    if (json[jsonKeyFeedUserBeanWorks] is String) {
      try {
        List list = jsonDecode(json[jsonKeyFeedUserBeanWorks]);
        works = list.map((e) => FeedUserWorkBean.fromJson(e)).toList();
      } catch (e) {
        print(e);
      }
    }
    if (json[jsonKeyFeedUserBeanIsVideoLike] is String) {
      isVideoLike = json[jsonKeyFeedUserBeanIsVideoLike] == '0' ? false : true;
    }
    if (json[jsonKeyFeedUserBeanLandMarks] is String) {
      try {
        List list = jsonDecode(json[jsonKeyFeedUserBeanLandMarks]);
        landmarks = list.map((e) => LandmarkBean.fromJson(e)).toList();
      } catch (e) {
        print(e);
      }
    }
    if (json[jsonKeyFeedUserBeanIsBlock] is String) {
      isBlock = json[jsonKeyFeedUserBeanIsBlock] == '0' ? false : true;
    }
    if (json[jsonKeyFeedUserBeanVideoTitle] is String) {
      videoTitle = json[jsonKeyFeedUserBeanVideoTitle];
    }
    if (json[jsonKeyFeedUserBeanVideoId] is String) {
      videoId = json[jsonKeyFeedUserBeanVideoId];
    }
    if (json[jsonKeyFeedUserBeanSortId] is int) {
      sortId = json[jsonKeyFeedUserBeanSortId];
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data[jsonKeyFeedUserBeanUserId] = userId;
    _data[jsonKeyFeedUserBeanNickname] = nickname;
    _data[jsonKeyFeedUserBeanAvatar] = avatar;
    _data[jsonKeyFeedUserBeanIsFriend] = isFriend;
    _data[jsonKeyFeedUserBeanAuthor] = author;
    _data[jsonKeyFeedUserBeanLocalVideo] = localVideo;
    _data[jsonKeyFeedUserBeanLocalVideoCover] = localVideoCover;
    _data[jsonKeyFeedUserBeanIsVideoLike] = isVideoLike;
    _data[jsonKeyFeedUserBeanIsBlock] = isBlock;
    _data[jsonKeyFeedUserBeanVideoTitle] = videoTitle;
    _data[jsonKeyFeedUserBeanVideoId] = videoId;
    _data[jsonKeyFeedUserBeanSortId] = sortId;
    if (works != null) {
      _data[jsonKeyFeedUserBeanWorks] = works?.map((e) => e.toJson()).toList();
    }
    if (landmarks != null) {
      _data[jsonKeyFeedUserBeanLandMarks] = landmarks?.map((e) => e.toJson()).toList();
    }
    return _data;
  }

  Map<String, dynamic> toDbJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data[jsonKeyFeedUserBeanUserId] = userId;
    _data[jsonKeyFeedUserBeanNickname] = nickname;
    _data[jsonKeyFeedUserBeanAvatar] = avatar;
    _data[jsonKeyFeedUserBeanIsFriend] = isFriend == true ? '1' : '0';
    _data[jsonKeyFeedUserBeanAuthor] = author;
    _data[jsonKeyFeedUserBeanLocalVideo] = localVideo;
    _data[jsonKeyFeedUserBeanLocalVideoCover] = localVideoCover;
    _data[jsonKeyFeedUserBeanIsVideoLike] = isVideoLike == true ? '1' : '0';
    _data[jsonKeyFeedUserBeanIsBlock] = isBlock == true ? '1' : '0';
    _data[jsonKeyFeedUserBeanVideoTitle] = videoTitle;
    _data[jsonKeyFeedUserBeanVideoId] = videoId;
    _data[jsonKeyFeedUserBeanSortId] = sortId;

    ///数组转字符串
    if (works is List) {
      _data[jsonKeyFeedUserBeanWorks] = jsonEncode(works);
    }

    if (landmarks is List) {
      _data[jsonKeyFeedUserBeanLandMarks] = jsonEncode(landmarks);
    }

    return _data;
  }

  FeedUserBean copyWith({
    String? userId,
    String? nickname,
    String? avatar,
    bool? isFriend,
    String? author,
    String? localVideo,
    String? localVideoCover,
    List<FeedUserWorkBean>? works,
    bool? isVideoLike,
    List<LandmarkBean>? landmarks,
    String? videoTitle,
    String? videoId,
    int? sortId,
  }) =>
      FeedUserBean(
        userId: userId ?? this.userId,
        nickname: nickname ?? this.nickname,
        avatar: avatar ?? this.avatar,
        isFriend: isFriend ?? this.isFriend,
        author: author ?? this.author,
        localVideo: localVideo ?? this.localVideo,
        localVideoCover: localVideoCover ?? this.localVideoCover,
        works: works ?? this.works,
        isVideoLike: isVideoLike ?? this.isVideoLike,
        landmarks: landmarks ?? this.landmarks,
        videoTitle: videoTitle ?? this.videoTitle,
        videoId: videoId ?? this.videoId,
        sortId: sortId ?? this.sortId,
      );
}

class FeedUserWorkBean {
  String? title;
  String? cover;
  List<String>? images;
  String? description;
  String? workId;
  String? isLike;

  FeedUserWorkBean({
    this.title,
    this.cover,
    this.images,
    this.description,
    this.workId,
    this.isLike,
  });

  FeedUserWorkBean.fromJson(dynamic json) {
    if (json["title"] is String) {
      title = json["title"];
    }
    if (json["cover"] is String) {
      cover = json["cover"];
    }
    if (json["images"] is List) {
      images = json["images"] == null ? null : List<String>.from(json["images"]);
    }
    if (json["description"] is String) {
      description = json["description"];
    }
    if (json["work_id"] is String) {
      workId = json["work_id"];
    }
    if (json["is_like"] is String) {
      isLike = json["is_like"];
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["title"] = title;
    _data["cover"] = cover;
    if (images != null) {
      _data["images"] = images;
    }
    _data["description"] = description;
    _data["work_id"] = workId;
    _data["is_like"] = isLike;
    return _data;
  }

  FeedUserWorkBean copyWith({
    String? title,
    String? cover,
    List<String>? images,
    String? description,
    String? workId,
    String? isLike,
  }) =>
      FeedUserWorkBean(
        title: title ?? this.title,
        cover: cover ?? this.cover,
        images: images ?? this.images,
        description: description ?? this.description,
        workId: workId ?? this.workId,
        isLike: isLike ?? this.isLike,
      );
}
